package com.tpyule.tpclass.service.impl;

import com.tpyule.tpclass.dao.RoleDao;
import com.tpyule.tpclass.entity.bo.MenuDetailBo;
import com.tpyule.tpclass.entity.bo.UserDetailBo;
import com.tpyule.tpclass.entity.dto.CommonQueryDto;
import com.tpyule.tpclass.entity.pojo.Menu;
import com.tpyule.tpclass.entity.pojo.Role;
import com.tpyule.tpclass.entity.pojo.User;
import com.tpyule.tpclass.entity.pojo.UserRole;
import com.tpyule.tpclass.service.MenuService;
import com.tpyule.tpclass.service.RoleService;
import com.tpyule.tpclass.service.UserService;
import com.tpyule.tpclass.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/5/4 11:22
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;
    @Autowired
    MenuService menuService;
    @Autowired
    UserService userService;

    @Override
    public int getSizeRole() {
        return roleDao.getSizeRole();
    }

    @Override
    public List<Role> getListRole(CommonQueryDto dto) {
        if(dto.getPage()>0&&dto.getPageSize()>0){
            dto.setStart((dto.getPage() - 1) * dto.getPageSize());
        }
        return roleDao.getListRole(dto);
    }

    @Override
    public List<Role> getAllRole() {
        return roleDao.getAllRole();
    }

    @Transactional
    @Override
    public void addRole(Role role, String backPaths, String managePaths) {
        roleDao.addRole(role);
        int[] bps = NumberUtil.StringToIntArray(backPaths);
        int[] mps = NumberUtil.StringToIntArray(managePaths);
        Menu backMenu = new Menu();
        backMenu.setRoleId(role.getId());
        backMenu.setType(3);
        menuService.insertMenu(backMenu,bps);
        Menu manageMenu = new Menu();
        manageMenu.setRoleId(role.getId());
        manageMenu.setType(4);
        menuService.insertMenu(manageMenu,mps);
    }

    @Transactional
    @Override
    public void updateRole(Role role, String backPaths, String managePaths) {
        int[] bps = NumberUtil.StringToIntArray(backPaths);
        int[] mps = NumberUtil.StringToIntArray(managePaths);
        List<MenuDetailBo> bos = menuService.getListMenu(role.getId());
        // 更新角色信息
        roleDao.updateRole(role);
        if (bos==null||bos.size()==0){
            Menu backMenu = new Menu();
            backMenu.setRoleId(role.getId());
            backMenu.setType(3);
            menuService.insertMenu(backMenu,bps);
            Menu manageMenu = new Menu();
            manageMenu.setRoleId(role.getId());
            manageMenu.setType(4);
            menuService.insertMenu(manageMenu,mps);

        }else {

            if (bos.get(0).getType()==3) {

                Menu m = new Menu();
                m.setId(bos.get(0).getId());
                menuService.updateMenuPath(m, bps);
            } else if (bos.get(0).getType()==4) {

                Menu m = new Menu();
                m.setId(bos.get(0).getId());
                menuService.updateMenuPath(m, mps);
            }


            if (bos.get(1).getType()==3) {

                Menu m = new Menu();
                m.setId(bos.get(1).getId());
                menuService.updateMenuPath(m, bps);
            } else if (bos.get(1).getType()==4) {

                Menu m = new Menu();
                m.setId(bos.get(1).getId());
                menuService.updateMenuPath(m, mps);
            }
        }
    }

    @Override
    public List<Role> getRoleByUserId(String userId) {
        return roleDao.getRoleByUserId(userId);
    }

    @Transactional
    @Override
    public void addUpdateUserRole(UserDetailBo bo) {
        User u = userService.getUserById(bo.getUserId());
        u.setPhone(bo.getPhone());
        u.setEmail(bo.getEmail());
        u.setNickname(bo.getNickname());
        u.setImageUrl(bo.getImageUrl());
        Boolean result = userService.updateUser(u);
        roleDao.deleteUserRoleByUId(bo.getUserId());
        for (int roleId : bo.getRoleIds()) {
            UserRole ur = new UserRole();
            ur.setUserId(bo.getUserId());
            ur.setRoleId(roleId);
            roleDao.addUserRole(ur);
        }
    }

    @Override
    public void addUserRole(UserRole userRole){
        roleDao.addUserRole(userRole);
    }
}
